/** 
 * PositionListener.java
 * @author Lex
 * 2007-9-12 ����06:51:57
 */
package org.lex.ftp.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.SwingConstants;

class PositionListener implements ActionListener {
	private final MainContentPane frame;

	PositionListener(MainContentPane frame) {
		this.frame = frame;
	}

	private int mainSplit = 0;
	private int topSplit = 0;
	private int bottomSplit = 0;

	public void actionPerformed(ActionEvent e) {
		JMenuItem menu = (JMenuItem) e.getSource();
		int pos = Integer.parseInt(menu.getName());
		if (menu.getActionCommand() == PosPane.MAXIMIZE)
			this.maximize(pos);
		else
			// restore�Ĺ����ƺ��Ƚϼ�
			this.setDividerLocation(mainSplit, topSplit, bottomSplit);
	}

	private void maximize(int position) {
		// backup split-pane's divider location
		this.mainSplit = this.frame.getMainSplitPane().getDividerLocation();
		this.topSplit = this.frame.getTopSplitPane().getDividerLocation();
		this.bottomSplit = this.frame.getBottomSplitPane().getDividerLocation();
		// do actual work
		switch (position) {
		case SwingConstants.SOUTH_EAST:
			this.setDividerLocation(0, topSplit, 0);
			return;
		case SwingConstants.SOUTH_WEST:
			this.setDividerLocation(0, topSplit, -1);
			return;
		case SwingConstants.NORTH_EAST:
			this.setDividerLocation(-1, 0, bottomSplit);
			return;
		case SwingConstants.NORTH_WEST:
			this.setDividerLocation(-1, -1, bottomSplit);
			return;
		}
	}

	/*
	 * "-1" ���������壬�ο������еĴ���
	 */
	private void setDividerLocation(int main, int top, int bottom) {
		if (-1 == main)
			main = this.frame.getMainSplitPane().getHeight();
		if (-1 == top)
			top = this.frame.getTopSplitPane().getWidth();
		if (-1 == bottom)
			bottom = this.frame.getBottomSplitPane().getWidth();
		this.frame.getMainSplitPane().setDividerLocation(main);
		this.frame.getTopSplitPane().setDividerLocation(top);
		this.frame.getBottomSplitPane().setDividerLocation(bottom);
	}
}
